package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.domain.Medicine;
import com.wang.mapper.MedicineMapper;
import com.wang.model.Medicine.MedicineSelectVM;
import com.wang.service.MedicineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WangZai
 * @since 2023-05-07
 */
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements MedicineService {

    @Override
    public boolean addMedicine(Medicine medicine) {
        return this.save(medicine);
    }

    @Override
    public boolean deleteMedicineById(Integer id) {
        return removeById(id);
    }

    @Override
    public boolean deleteMedicineByLargeCategoryId(Integer largeCategoryId) {
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("large_category_id",largeCategoryId);
        return this.remove(queryWrapper);
    }

    @Override
    public boolean deleteMedicineBySmallCategoryId(Integer smallCategoryId) {
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("small_category_id",smallCategoryId);
        return this.remove(queryWrapper);
    }

    @Override
    public Medicine getMedicineByName(String name) {
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("medicine_name",name);
        return this.getOne(queryWrapper);
    }

    @Override
    public Medicine getMedicineById(Integer medicineId) {
        return this.getById(medicineId);
    }

    @Override
    public boolean updateMedicine(Medicine medicine) {
        return this.updateById(medicine);
    }

    @Override
    public List<Medicine> conditionSelect(MedicineSelectVM model) {
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        if(model.getMedicineId()==null&&model.getMedicineName()==null&&model.getLargeCategoryId()==null&&model.getSmallCategoryId()==null){
            return this.list();
        }else {
            if(model.getMedicineId() != null){
                queryWrapper.like("medicine_id",model.getMedicineId());
            }
            if(!"".equals(model.getMedicineName())){
                queryWrapper.like("medicine_name",model.getMedicineName());
            }

            if(model.getLargeCategoryId() != null){
                queryWrapper.like("large_category_id",model.getLargeCategoryId());
            }

            if(model.getSmallCategoryId() != null){
                queryWrapper.like("small_category_id",model.getSmallCategoryId());
            }
            return this.list(queryWrapper);
        }
    }

    @Override
    public List<Medicine> getListBySmallCategoryId(Integer smallCategoryId) {
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("small_category_id",smallCategoryId);
        return this.list(queryWrapper);
    }

    @Override
    public List<Medicine> getListBySmallCategoryIdAndDimName(Integer smallCategoryId, String medicineName) {
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("small_category_id",smallCategoryId);
        queryWrapper.like("medicine_name",medicineName);
        return this.list(queryWrapper);
    }

    @Override
    public List<Medicine> getList() {
        return this.list();
    }

}
